

#include "CountDownLatch.hpp"

namespace tulun
{
    // class CountDownLatch::
    // std::atomic_int count_;
    // std::mutex mutex_;
    // std::condition_variable cond_;

    CountDownLatch::CountDownLatch(int count)
        : count_(count)
    {
    }
    CountDownLatch::~CountDownLatch()
    {
    }
    void CountDownLatch::wait()
    {
        std::unique_lock<std::mutex> locker(mutex_);
        while (count_ > 0)
        {
            cond_.wait(locker);
        }
    }
    void CountDownLatch::countDown()
    {
        std::unique_lock<std::mutex> locker(mutex_);
        --count_;
        if (count_ == 0)
        {
            cond_.notify_all();
        }
    }
    int CountDownLatch::getCount() const
    {
        return count_.load();
    }

} // namespace tulun